package cn.edu.uestc.smgt.controller.yh;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import cn.edu.uestc.smgt.pojo.User;
import cn.edu.uestc.smgt.service.BuzhuCurrentService;
import cn.edu.uestc.smgt.util.ExcelUtil;

@Controller
@RequestMapping("/yh/BZ/")
public class DanweiBuZhuController {

	private BuzhuCurrentService buzhuCurrentService;

	/**
	 * 列出本单位所有未提交的补助信息
	 */
	@RequestMapping("list_WTJ")
	@ResponseBody
	public Object listBySelections(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws IOException {
		User yh = (User) session.getAttribute("user");
		if (yh != null) {
			return buzhuCurrentService.dwZG_WTJ(yh.getUnitId());
		} else {
			return null;
		}
	}

	/**
	 * 上传助管补助信息
	 */
	@Transactional(rollbackFor = { Exception.class })
	@RequestMapping("upload")
	@ResponseBody
	public Object upload(HttpServletRequest request, HttpServletResponse response, HttpSession session,
			@RequestParam("uploadExcel") CommonsMultipartFile files) throws IOException {
		System.out.println(files.getOriginalFilename());
		HashMap ret = new HashMap();
		int fileSize = 0;
		if (files != null) {
			int rowNum = 0;
			try {
				// 将excel文件转为List，然后调用service层方法批量插入
				InputStream is = files.getInputStream();
				HashMap sheetMap = ExcelUtil.getSheet(is, 1);
				if (sheetMap.get("sheet") != null) {
					Sheet sheet = (Sheet) sheetMap.get("sheet");
					List list = readZGBZData(sheet);
					if (null != list)
						fileSize = list.size();
					// 导入数据，如果有已经存在数据库中的数据则不导入
					for (int i = 0; i < list.size(); i++) {
						HashMap map = (HashMap) list.get(i);
						rowNum = ((Integer) map.get("row")).intValue();
						buzhuCurrentService.addWithMap(map);
					}
				} else {
					ret.put("message", "数据为空");
					return ret;
				}
				is.close();
			} catch (Exception e1) {
				e1.printStackTrace();
				if (rowNum == fileSize + 1) {
					ret.put("message", "导入数据成功");
					return ret;
				} else {
					ret.put("message", "第" + rowNum + "行数据异常，请检查！");
					return ret;
				}
			}
			ret.put("message", "导入数据成功");
			return ret;
		} else {
			ret.put("message", "未选择文件！");
			return ret;
		}

	}

	/**
	 * 将上传的excel文件转为list
	 * 
	 * @param sheet
	 * @return
	 */
	private List readZGBZData(Sheet sheet) {
		// TODO Auto-generated method stub
		List list = new ArrayList();
		for (int i = 1; i <= sheet.getLastRowNum(); i++) {
			HashMap map = new HashMap();
			int j = 0;
			int nullCount = 0;
			String stuId = ExcelUtil.getCellValue(sheet, j++, i);
			if (!ExcelUtil.isNullOrEmpty(stuId)) {
				map.put("stuId", stuId);
			} else {
				nullCount++;
			}
			String stuName = ExcelUtil.getCellValue(sheet, j++, i);
			if (!ExcelUtil.isNullOrEmpty(stuName)) {
				map.put("stuName", stuName);
			} else {
				nullCount++;
			}
			String unitId = ExcelUtil.getCellValue(sheet, j++, i);
			if (!ExcelUtil.isNullOrEmpty(unitId)) {
				map.put("unitId", unitId);
			} else {
				nullCount++;
			}
			String unitName = ExcelUtil.getCellValue(sheet, j++, i);
			if (!ExcelUtil.isNullOrEmpty(unitName)) {
				map.put("unitName", unitName);
			} else {
				nullCount++;
			}
			String officeName = ExcelUtil.getCellValue(sheet, j++, i);
			if (!ExcelUtil.isNullOrEmpty(officeName)) {
				map.put("officeName", officeName);
			} else {
				nullCount++;
			}
			String bankName = ExcelUtil.getCellValue(sheet, j++, i);
			if (!ExcelUtil.isNullOrEmpty(bankName)) {
				map.put("bankNo", bankName);
			} else {
				nullCount++;
			}
			String bankNo = ExcelUtil.getCellValue(sheet, j++, i);
			if (!ExcelUtil.isNullOrEmpty(bankNo)) {
				map.put("bankNo", bankNo);
			} else {
				nullCount++;
			}
			String stuTel = ExcelUtil.getCellValue(sheet, j++, i);
			if (!ExcelUtil.isNullOrEmpty(stuTel)) {
				map.put("stuTel", stuTel);
			} else {
				nullCount++;
			}
			String yearMonth = ExcelUtil.getCellValue(sheet, j++, i);
			if (!ExcelUtil.isNullOrEmpty(yearMonth)) {
				map.put("yearMonth", yearMonth);
			} else {
				nullCount++;
			}
			String money = ExcelUtil.getCellValue(sheet, j++, i);
			if (!ExcelUtil.isNullOrEmpty(money)) {
				map.put("money", money);
			} else {
				nullCount++;
			}
			String beizhu = ExcelUtil.getCellValue(sheet, j++, i);
			if (!ExcelUtil.isNullOrEmpty(beizhu)) {
				map.put("beizhu", beizhu);
			} else {
				nullCount++;
			}
			if (nullCount != j) {
				map.put("row", Integer.valueOf(i + 1));
				list.add(map);
			}
		}
		return list;
	}

}
